package com.smh.server;

import com.smh.User;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Server {
    public static void main(String[] args) throws IOException {

        //获取文件中的用户信息
        //这里也可以用Properties;
        ArrayList<User> users = new ArrayList<>();
        File file = new File("sockethomework\\save\\UserInfo.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String s;
        while ((s = br.readLine()) != null) {
            String[] split = s.split("=");
            users.add(new User(split[0], split[1]));//获取数据并添加到集合中
        }

        //创建服务器对象,指定接收端口
        ServerSocket ss = new ServerSocket(6666);
        //创建集合存储socket对象
        ArrayList<Socket> list = new ArrayList<>();

        //接受链接
        while (true) {
            Socket socket = ss.accept();
            if (socket != null) {
                //添加到集合中
                list.add(socket);
                //开启线程去处理登录和注册请求
                ServerThread serverThread = new ServerThread(socket, users, list);
                serverThread.start();
            }
        }


    }
}
